import scrapy
from scrapy import Request, FormRequest

class LoginSpider(scrapy.Spider):
    name = 'login2'
    allowed_domains = ['17k.com']
    start_urls = ['https://user.17k.com/www/']

    def start_requests(self):
        """首次发送请求：进行登录"""

        # 用户名和密码
        username = "18370942865"
        password = "zxydsg123"

        # 登录界面的 URL
        url = "https://passport.17k.com/"

        # 发送 post请求 -- 拿到 cookie值
        yield FormRequest(
            url=url,
            formdata={
                "user": username,
                "pass": password
            }, # key值 与 页面保持一致
            callback=self.parse # 回调函数
        )

        """
        # 另一种方式：发送post请求
        yield Request(
            url=url,
            method="post",
            body="user=18370942865&pass=zxydsg123",
            callback=self.parse
        )
        """


    def parse(self, response):
        """解析数据"""

        # 发送请求
        yield Request(
            url=LoginSpider.start_urls[0],
            callback=self.parse_detail # 回调函数
        )

    def parse_detail(self, resp):
        """检查是否登录成功"""
        print(resp)